import Vue from 'vue'
import Router from 'vue-router'
import App from './App.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'app',
      redirect: '/home',
      component: App,
      children: [
        {
          path: 'home',
          name: 'home',
          component: () => import('./views/Header'),
          children: [
            {
              path: 'user',
              name: 'user',
              component: () => import('./views/sidebar/User'),
              children: [
                {
                  path: 'nav-module/:id',
                  name: 'nav-module',
                  meta: {
                    title: 'U教云'
                  },
                  component: () => import('./views/NavModule')
                }
              ]
            },
            {
              path: 'module',
              name: 'module',
              component: () => import('./views/sidebar/Module'),
              children: [
                {
                  path: 'schedule',
                  name: 'schedule',
                  meta: {
                    title: '日程',
                    roles: ['module']
                  },
                  component: () => import('./views/modules/schedule/Schedule') // 日程模块
                },
                {
                  path: 'screen-manage',
                  name: 'screen-manage',
                  redirect: 'screen-manage/custom-screen',
                  meta: {
                    title: '云屏管理'
                  },
                  component: () => import('./views/modules/screenManage/App'), // 云屏管理模块
                  children: [
                    {
                      path: 'custom-screen',
                      name: 'CustomScreen',
                      meta: {
                        title: '云屏自定义'
                      },
                      component: () => import('./views/modules/screenManage/CustomScreen') // 云屏自定义
                    }
                  ]
                },
                {
                  path: 'not-auth',
                  name: 'NotAuth',
                  meta: {
                    title: 'U教云|404'
                  },
                  component: () => import('_views/NotAuth') // 无权限页面
                }
              ]
            }
          ]
        }
      ]
    },
    {
      path: '*',
      name: 'NotFound',
      meta: {
        title: 'U教云|404'
      },
      component: () => import('_views/NotFound') // 404页面
    }
  ]
});
router.beforeEach((to, from, next) => {
  // 修改title
  document.title = to.meta.title;
  // 获取当前登陆账号的身份
  const UserRoles = JSON.parse(localStorage.getItem('roles'));
  if (to.meta.roles && !UserRoles.includes(to.meta.roles)) {
    router.push('/home/module/not-auth')
  }
  next();
});
export default router
